<template>
  <div class="user-profile-container">
    <div class="profile-card">
      <h3 class="card-title">个人信息</h3>
      <el-form :model="customer" label-width="120px" class="custom-form">
        <el-form-item label="用户名">
          <el-input v-model="customer.userName" disabled class="form-input"></el-input>
        </el-form-item>
        <el-form-item label="密码">
          <el-input v-model="customer.password" show-password placeholder="当前密码" class="form-input"></el-input>
        </el-form-item>
        <el-form-item label="姓名">
          <el-input v-model="customer.name" class="form-input"></el-input>
        </el-form-item>
        <el-form-item label="性别">
          <el-input v-model="customer.sex" placeholder="男0女1" class="form-input"></el-input>
        </el-form-item>
        <el-form-item label="年龄">
          <el-input v-model="customer.age" class="form-input"></el-input>
        </el-form-item>
        <el-form-item label="地址">
          <el-input v-model="customer.address" class="form-input"></el-input>
        </el-form-item>
        <el-form-item label="身份证号">
          <el-input v-model="customer.idCard" class="form-input"></el-input>
        </el-form-item>
        <el-form-item class="button-group">
          <el-button type="primary" @click="updateUserInfo" class="custom-button">
            <i class="el-icon-edit"></i> 更新个人信息
          </el-button>
          <el-button type="primary" @click="goback" class="custom-button secondary">
            <i class="el-icon-back"></i> 返回
          </el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script lang="ts" setup>
import axios from "axios";
import { ref, onMounted } from "vue";
import { ElMessage } from "element-plus";
import { store } from "@/common/store";
import router from "@/router";

const customer = ref({
  userName: "",
  password: "",
  name: "",
  sex: "",
  age: "",
  address: "",
  idCard: "",
  status: 0,
  createTime: "",
});

// 获取个人信息
const fetchUserInfo = async () => {
  try {
    const response = await axios.get("/tCustomer/info", {
      params: {
        username: store.userinfo.username,
      },
    });
    if (response.data.code === 200) {
      customer.value = {
        ...customer.value,
        ...response.data.data,
      };
      // ElMessage.success("获取个人信息成功！");
    } else {
      ElMessage.error(response.data.message);
    }
  } catch (error) {
    console.error("获取个人信息失败:", error);
    ElMessage.error("获取个人信息失败，请重试");
  }
};

// 更新个人信息
const updateUserInfo = async () => {
  try {
    const response = await axios.put("/tCustomer/info", customer.value);
    if (response.data.code === 200) {
      ElMessage.success("个人信息更新成功！");
    } else {
      ElMessage.error(response.data.message);
    }
  } catch (error) {
    console.error("更新个人信息失败:", error);
    ElMessage.error("更新个人信息失败，请重试");
  }
};

function goback() {
  router.back();
}

// 在组件挂载完成后自动调用获取用户信息的方法
onMounted(() => {
  fetchUserInfo();
});
</script>

<style scoped>
/* 页面容器样式 */
.user-profile-container {
  min-height: 100vh;
  padding: 30px 0;
  background: linear-gradient(135deg, #f5f7fa 0%, #e4eaf3 100%);
  display: flex;
  justify-content: center;
  align-items: flex-start;
}

/* 卡片样式 */
.profile-card {
  width: 90%;
  max-width: 650px;
  background: white;
  border-radius: 12px;
  box-shadow: 0 10px 25px rgba(0, 0, 0, 0.05), 0 6px 6px rgba(0, 0, 0, 0.07);
  padding: 30px 40px;
  transition: transform 0.3s ease, box-shadow 0.3s ease;
}

.profile-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 15px 30px rgba(0, 0, 0, 0.1), 0 8px 10px rgba(0, 0, 0, 0.08);
}

/* 卡片标题样式 */
.card-title {
  font-size: 24px;
  font-weight: 600;
  color: #303133;
  margin-bottom: 25px;
  text-align: center;
  position: relative;
}

.card-title::after {
  content: "";
  position: absolute;
  bottom: -10px;
  left: 50%;
  transform: translateX(-50%);
  width: 60px;
  height: 3px;
  background: #409eff;
  border-radius: 3px;
}

/* 表单样式 */
.custom-form .el-form-item {
  margin-bottom: 20px;
}

.form-input .el-input__inner {
  border-radius: 6px;
  padding: 10px 15px;
  height: auto;
  transition: border-color 0.3s ease, box-shadow 0.3s ease;
}

.form-input .el-input__inner:focus {
  border-color: #409eff;
  box-shadow: 0 0 0 2px rgba(64, 158, 255, 0.2);
}

/* 按钮样式 */
.button-group {
  display: flex;
  justify-content: center;
  margin-top: 30px;
}

.custom-button {
  margin: 0 10px;
  padding: 10px 25px;
  border-radius: 6px;
  font-size: 16px;
  transition: all 0.3s ease;
}

.custom-button:hover {
  transform: translateY(-2px);
}

.custom-button.secondary {
  background-color: #f5f7fa;
  border-color: #dcdfe6;
  color: #606266;
}

.custom-button.secondary:hover {
  background-color: #e4e7ed;
}

/* 响应式布局 */
@media (max-width: 576px) {
  .profile-card {
    padding: 20px;
    width: 95%;
  }
  
  .button-group {
    flex-direction: column;
  }
  
  .custom-button {
    margin: 5px 0;
  }
}
</style>